<?php

App::uses('AppModel', 'Model');

class Category extends AppModel {

    public $belongsTo = array(
        'ParentCategory' => array(
            'className' => 'Category',
            'foreignKey' => 'parent_id'
        )
    );

    public function tree($parent_id = null) {
        $categories = $this->find('all', array(
            'fields'     => array('Category.*'),
            'conditions' => array( 'Category.parent_id' => $parent_id )
        ));
        foreach ($categories as &$category) {
            $category['CategoryChildren'] = $this->tree($category['Category']['id']);
        }
        
        return $categories;
    }

}
